import { name } from "pubsub-js"

/**
 * id: 菜单主键id children为[]时，id与path需保持一致
 * name: 菜单名称
 * path: 路由跳转路径
 * children: 子菜单
 */
export const menuJson = [
  // {
  //   id: "/home",
  //   name: "Home",
  //   path: "/home",
  //   children: [],
  // },
  {
    id:'/Data_preprocessing',
    name:'Data Upload',
    path:'/Data_preprocessing',
    children:[],
  },
  {
    id:"/Species_composition",
    name:"Species Identification",
    path:"/Species_composition",
    children:[
      {
        id:"/identification",
        name:"Identification",
        path:"/identification",
        children:[],
      },
      {
        id:"/Krona",
        name:"Krona",
        path:"/Krona",
        children:[],
      },
      {
        id:"/Sankey",
        name:"Sankey",
        path:"/Sankey",
        children:[],
      },
      {
        id:"/Tree",
        name:"Tree",
        path:"/Tree",
        children:[],
      },
    ],
  },
  {
    id: "/Species_annotation",
    name: "Taxonomic identification",
    path: "/Species_annotation",
    children: [
      {
        id:"/kraken",
        name:"Species Abundance",
        path:"/kraken",
        children:[],
      },
      // {
      //   id: "/test",
      //   name: "test",
      //   path: "/test",
      //   children:[],
      // },
      {
        id: "/plot",
        name: "Plot",
        path: "/plot",
        children:[],
      },
    ],
  },
  {
    id: "/Species_Diversity",
    name: "Species Diversity",
    path: "/Species_Diversity",
    children: [
      {
        id: "/alpha_diversity",
        name: "Alpha Diversity",
        path: "/alpha_diversity",
        children:[
          {
            id: "/alpha_diversity",
            name: "Alpha Diversity",
            path: "/alpha_diversity",
            children:[],
          },
          {
            id: "/alpha_plot",
            name: "Alpha Plot",
            path: "/alpha_plot",
            children:[],
          },
        ],
      },
      {
        id: "/beta_diversity",
        name: "Beta Diversity",
        path: "/beta_diversity",
        children:[
          {
            id: "/beta_diversity",
            name: "Beta Diversity",
            path: "/beta_diversity",
            children:[],
          },
          {
            id: "/BC_plots",
            name: "Bray-Curtis Plots",
            path: "/BC_plots",
            children:[],
          },
        ],
      },  
    ],
  },
  {
    id: "/functional_annotation",
    name: "VFDB Annotation",
    path: "/functional_annotation",
    children: [],
  },
  // {
  //   id: "/specise",
  //   name: "鉴定",
  //   path: "/specise",
  //   children: [],
  // },
  // {
  //   id: "/Data",
  //   name: "数据",
  //   path: "/data",
  //   children: [],
  // },

  // {
  //   id: "/Echarts",
  //   name: "Analysis",
  //   path: "/Echarts",
  //   children: [
  //         {
  //           id: "/kraken2Index",
  //           name: "Kraken2",
  //           path: "/kraken2Index",
  //           children: [],
  //         },
  //         {
  //           id: "/Test",
  //           name: "Test",
  //           path: "/Test",
  //           children: [],
  //         },
  //         {
  //           id: "/BasiceChart",
  //           name: "BasiceChart",
  //           path: "/BasiceChart",
  //           children: [],
  //         },
  //         {
  //           id: "/DataTable",
  //           name: "Table",
  //           path: "/DataTable",
  //           children: [],
  //         },
  //         {
  //           id: "/Datatest",
  //           name: "Test",
  //           path: "/Datatest",
  //           children: [],
  //         },
  //         {
  //           id: "/DataScatter",
  //           name: "Scatter",
  //           path: "/DataScatter",
  //           children: [],
  //         },
  //         {
  //           id: "/DataHeatmap",
  //           name: "Heatmap",
  //           path: "/DataHeatmap",
  //           children: [],
  //         },
  //         {
  //           id: "/DataRelation",
  //           name: "Relation",
  //           path: "/DataRelation",
  //           children: [],
  //         },
  //       ],
  // },
  {
    id: "/jbrowse",
    name: "JBrowse",
    path: "/jbrowse",
    children: [
      {
        id: "/jbrowse",
        name: "jbrowse",
        path: "/jbrowse",
        children: [],
      },
    ],
  }
]

// 返回菜单第一层级id，用于antd中展开项
export const getMenuFirstLevelId = () => {
  let arr = []
  menuJson.forEach(item=>{
      arr.push(item.id)
  })
  return arr
}
// 递归获取第一项
const OpenFisrt = initOpenFisrt()

// 默认展开项
export const initOpenKeys = OpenFisrt.OpenKeys

// 默认选中项
export const initSelectedKeys = OpenFisrt.SelectedKeys

// 默认显示面包屑
export const initBreadcrumb = OpenFisrt.Breadcrumb

// 默认显示选项卡
export const initTabs = OpenFisrt.Tabs

// 登录进入首页或路由地址为'/',默认打开菜单第一项
function initOpenFisrt() {
  let OpenKeys = [],
      SelectedKeys = [],
      Breadcrumb = []
  recursionGetFisrt(menuJson[0], OpenKeys, SelectedKeys, Breadcrumb)
  return {
    OpenKeys,
    SelectedKeys,
    Breadcrumb,
    Tabs: [{
      title: Breadcrumb[Breadcrumb.length-1],
      key: SelectedKeys[0],
      closable: false,
      openKeys: OpenKeys,
      selectedKeys: SelectedKeys,
      breadcrumb: Breadcrumb
    }]
  }
}
function recursionGetFisrt (data, OpenKeys, SelectedKeys, Breadcrumb) {
    let children = data.children
    Breadcrumb.push(data.name)
    if(children && children.length && children.length > 0){
      OpenKeys.push(data.id)
      recursionGetFisrt(children[0], OpenKeys, SelectedKeys, Breadcrumb)
    }else{
      SelectedKeys.push(data.id)
    }
}


// 用于监听浏览器前进后退，根据id获取当前展开项
export const getOpenKeysForId = (id) => {
  return recursionData(menuJson, id).arr
}
// 递归数据
const recursionData = (data, id) => {
  let arr = [], isFind = false
  for(var i in data){
    let cld = data[i].children
    arr = []
    if(cld.length && cld.length > 0){
      arr.push(data[i].id)
      let o = recursionData(cld, id)
      arr = [...arr, ...o.arr]
      if(o.isFind) return {isFind: o.isFind, arr}
    }else{
      if(data[i].path == id){
        isFind = true
        break
      }
    }
  }
  return {isFind, arr}
}

// 更新面包屑显示文字
export const getKeyPathTitle = (keyPath) => {
  let title = []
  keyPath.forEach(item => {
      let str = returnName(menuJson, item)
      // 如果没有找到，则显示404
      title.push(str || item.split('/')[1])
  })
  // 返回菜单名称
  return title
}

// 递归查询每一层级的name值
const returnName = (data, value)=>{
  let name = ''
  for(var i in data){
      if(data[i].id == value){
          name = data[i].name
      }
      if(!name && data[i].children.length > 0){
          name = returnName(data[i].children, value)
      }
      if(name) {
          break
      }
  }
  return name
}